package com.console.example;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class ConsoleShow {
    private static final String[] ASCII_ART = {
        "⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄",
        "▓▓░░▓▓░░ ░░▓▓ ▓▓░░▓▓ ▓▓▓▓▓▓ ▓▓▓▓ ▓▓▓▓▓▓ ▓▓ ▓▓ ▓▓▓▓▓▓",
        "▄▄▄█████▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄"
    };

    private static final String[] QUOTES = {
        "真正的装逼，是让代码自己说话",
        "while(true) { System.out.println('又装到了'); }",
        "// 这段代码价值一个亿"
    };

    public static void main(String[] args) throws InterruptedException {
        int speed = args.length > 0 ? Integer.parseInt(args[0]) : 10;
        long delayMs = 1000 / speed;

        System.out.println("\033[33m炫酷控制台输出器 已启动 | 速度：" + speed + "帧/秒\033[0m");

        Random rand = new Random();
        while (true) {
            switch (rand.nextInt(3)) {
                case 0:
                    showMatrixEffect(delayMs);
                    break;
                case 1:
                    showProgressBar(delayMs);
                    break;
                case 2:
                    showAsciiArt(delayMs);
                    break;
            }
        }
    }

    private static void showMatrixEffect(long delay) throws InterruptedException {
        System.out.print("\033[2J\033[H"); // 清屏
        Random rand = new Random();
        int width = 80;
        int height = 24;

        for (int y = 0; y < height; y++) {
            StringBuilder line = new StringBuilder();
            for (int x = 0; x < width; x++) {
                if (rand.nextDouble() < 0.1) {
                    line.append("\033[32m") // 绿色
                       .append((char)(33 + rand.nextInt(94)))
                       .append("\033[0m");
                } else {
                    line.append(' ');
                }
            }
            System.out.println(line);
        }
        TimeUnit.MILLISECONDS.sleep(delay);
    }

    private static void showProgressBar(long delay) throws InterruptedException {
        System.out.print("\033[2J\033[H");
        int width = 50;
        for (int i = 0; i <= width; i++) {
            String bar = "[" + 
                "\033[32m" + repeatString('=', i) + 
                "\033[37m" + repeatString(' ', width - i) + "] " + 
                (i*2) + "%\r";
            System.out.print(bar);
            System.out.flush();
            TimeUnit.MILLISECONDS.sleep(delay);
        }
    }

    private static void showAsciiArt(long delay) throws InterruptedException {
        System.out.print("\033[2J\033[H");
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println("\033[3" + rand.nextInt(8) + "m" + 
                ASCII_ART[rand.nextInt(ASCII_ART.length)]);
        }
        System.out.println("\033[33m" + QUOTES[rand.nextInt(QUOTES.length)]);
        TimeUnit.MILLISECONDS.sleep(delay * 5);
    }

    private static String repeatString(char c, int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(c);
        }
        return sb.toString();
    }
}
